package com.newworld.dmsapp2.ui.view;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.newworld.dmsapp2.R;
import com.newworld.dmsapp2.bean.Task;
import com.newworld.dmsapp2.bean.User;
import com.newworld.dmsapp2.fragment.bean.InitTextItemEntity;
import com.newworld.dmsapp2.util.Config;
import com.newworld.dmsapp2.util.HttpUtil;
import com.newworld.dmsapp2.util.SingleSample;
import com.newworld.dmsapp2.util.StringUtil;
import com.newworld.dmsapp2.util.Tool;

public class CustomTextItem extends LinearLayout{
	
	public CustomTextItem(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	LayoutInflater mInflater;
	View customTextItem;
	TextView tab,tx1,tx2;
	
	
	private InitTextItemEntity textItem = new InitTextItemEntity();
	private Context context;
	private Task task;
	private User user;
	private String sessionId;
	private int sizeType;//判断条目显示类型0:大，1:小
	
	public LinearLayout getCustomTextItem(Context context,InitTextItemEntity textItem,Task task,int sizeType) {
		// TODO Auto-generated constructor stub
		this.context = context;
		this.textItem = textItem;
		this.task = task;
		this.sizeType = sizeType;
		
		mInflater = LayoutInflater.from(context);
		if(sizeType == 0){//显示大字体
			customTextItem = mInflater.inflate(R.layout.view_custom_textitem, null);
		}else{//显示小字体
			customTextItem = mInflater.inflate(R.layout.view_custom_textitem_small, null);
		}
		initData();
		initView(customTextItem);
		initEvent();
		return (LinearLayout) customTextItem;
	}
	
	private void initData(){
		user = SingleSample.getInstance(context).getUser();
		if (null != user) {
			if (null != user.getSessionId() && !user.getSessionId().equals("")) {
				sessionId = user.getSessionId();
			}
		}
	}
	private void initView(View customTextItem) {
		if(sizeType == 0){//显示大字体
			tab = (TextView) customTextItem.findViewById(R.id.custom_textitem_tab);
			tx1 = (TextView) customTextItem.findViewById(R.id.custom_textitem_tx1);
			tx2 = (TextView) customTextItem.findViewById(R.id.custom_textitem_tx2);
		}else{//显示小字体
			tab = (TextView) customTextItem.findViewById(R.id.custom_textitem_small_tab);
			tx1 = (TextView) customTextItem.findViewById(R.id.custom_textitem_small_tx1);
			tx2 = (TextView) customTextItem.findViewById(R.id.custom_textitem_small_tx2);
		}
		
	}
	
	private void initEvent(){
		if(StringUtil.isStringAvailable(textItem.getTab())){
			tab.setText(textItem.getTab());
		}
		if(StringUtil.isStringAvailable(textItem.getTx1())){
			tx1.setText(textItem.getTx1());
		}else{
			tx1.setVisibility(View.GONE);
		}
		if(StringUtil.isStringAvailable(textItem.getTx2())){
			tx2.setText(textItem.getTx2());
			tx2.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
			tx2.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
		}else{
			tx2.setVisibility(View.GONE);
		}
		
		//电话拨打点击事件
		tx2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if(StringUtil.isStringEqual(tx2.getText().toString(), "催单")){
					remindOrder();
				}else{//拨打电话
					Intent intent;
					intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
							+ tx2.getText().toString()));
					context.startActivity(intent);
				}
				
			}
		});
		
	}

		/**
		 * 催单功能
		 */
		private void remindOrder() {
			// TODO Auto-generated method stub
			new AsyncTask<Void, Void, Void>() {

				private String flag;
				private String message;

				@Override
				protected void onPreExecute() {
					if (!Tool.isNetworkAvailable(context)) {
						Toast.makeText(context, "请检查网络连接！",
								Toast.LENGTH_SHORT).show();
					}
				}

				@Override
				protected Void doInBackground(Void... params) {
					if (Tool.isNetworkAvailable(context)) {
						try {
							String url = Tool.getUrlFromUrl(user, textItem.getOperationCode());
							Map<String, String> map = new HashMap<String, String>();
							map.put("functionCode",
									Config.FUNCTION_REAL_GETPROGRESSREMIND);
							map.put("repairId", task.getRepairId());
							
							String result = HttpUtil.postRequestSe(url, map,
									sessionId);
							Log.e("催单result", result);
							flag = new JSONObject(result).getString("flag");
							message = new JSONObject(result).getString("message");

						} catch (Exception e) {
							message = "json数据获取出错";
							e.printStackTrace();
						}
					} else {
						message = "请设置网络";
					}

					return null;
				}
				@Override
				protected void onPostExecute(Void result) {
					Toast.makeText(context, message,
							Toast.LENGTH_SHORT).show();
					if (flag.equals("success")) {
						tx2.setTextColor(getResources().getColor(
								R.color.nw_tx_gray));
					}

				}
			}.execute(null, null, null);
		}
		
}
